/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.propertiesController;

import java.awt.Font;
import java.util.Properties;
import systole.ioHandling.fileReaders.FilePropertiesReader;
import systole.processor.correlation.CorrelationKind;

/**
 *
 * @author jmj
 */
public abstract class SystoleProperties {

    protected Properties properties;
    protected FilePropertiesReader propertiesReader;

    /**
     * 
     */
    public SystoleProperties() {
        this.propertiesReader = new FilePropertiesReader();
        this.loadProperties();
    }

    protected abstract void loadProperties();

    /**
     * 
     */
    public abstract void saveProperties();

    protected void saveFont(String propertyName, Font font) {
        properties.put(propertyName + ".font.name", font.getName());
        properties.put(propertyName + ".font.style", String.valueOf(font.getStyle()));
        properties.put(propertyName + ".font.size", String.valueOf(font.getSize()));
    }

    protected Font loadFont(String propertyName) {
        String name = properties.getProperty(propertyName + ".font.name", "Arial");
        String sizeAux = properties.getProperty(propertyName + ".font.size", "12");
        String styleAux = properties.getProperty(propertyName + ".font.style", "0");
        int size;
        int style;
        try {
            size = Integer.parseInt(sizeAux);
        } catch (NumberFormatException e) {
            size = 12;
        }

        try {
            style = Integer.parseInt(styleAux);
        } catch (NumberFormatException e) {
            style = 0;
        }
        return new Font(name, style, size);
    }

    protected void saveIntValue(String key, int value) {
        this.properties.put(key, String.valueOf(value));
    }

    protected void saveBooleanValue(String key, boolean value) {
        this.properties.put(key, (value ? "1" : "0"));

    }

    protected void saveStringValue(String key, String value) {
        this.properties.put(key, value);
    }

    protected int loadIntValue(String key) {
        String value = this.properties.getProperty(key, "0");
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    protected boolean loadBooleanValue(String key) {
        String valueS = this.properties.getProperty(key, "0");
        try {
            int value = Integer.parseInt(valueS);
            return (value == 1);
        } catch (NumberFormatException e) {
            return false;
        }
    }

    protected String loadStringValue(String key) {
        return this.properties.getProperty(key, null);
    }

    protected void saveCorrelationEnum(String key, CorrelationKind value) {
        switch (value) {
            case RESULTADO:
                this.saveIntValue(key, 1);
                break;
            case SEGMENTO:
                this.saveIntValue(key, 0);
        }
    }

    protected CorrelationKind loadCorrelationEnum(String key) {
        int value = this.loadIntValue(key);
        switch (value) {
            case 1:
                return CorrelationKind.RESULTADO;
            default:
                return CorrelationKind.SEGMENTO;
        }
    }
}

